//
//  RootViewController.m
//  ImageTableViewBackGround
//
//  Created by Naoki TSUTSUI on 10/02/02.
//  Copyright iphoneworld.jp 2010. All rights reserved.
//

#import "RootViewController.h"
#import "NKDataSouce.h"
// #import "RootViewCell.h"

/*
 Predefined colors to alternate the background color of each cell row by row (see tableView:cellForRowAtIndexPath: and tableView:willDisplayCell:forRowAtIndexPath:).
 */
#define DARK_BACKGROUND  [UIColor colorWithRed:151.0/255.0 green:152.0/255.0 blue:155.0/255.0 alpha:1.0]
#define LIGHT_BACKGROUND [UIColor colorWithRed:172.0/255.0 green:173.0/255.0 blue:175.0/255.0 alpha:1.0]


@implementation RootViewController
@synthesize dataSource, rootCell;

/*
- (void)createToolbarItems
{	
	// match each of the toolbar item's style match the selection in the "UIBarButtonItemStyle" segmented control
	UIBarButtonItemStyle style;
  style = UIBarButtonItemStyleBordered;

  
	// create the system-defined "OK or Done" button
  UIBarButtonItem *systemItem
  systemItem = [[UIBarButtonItem alloc] 
                initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                target:self 
                action:@selector(action:)];
	systemItem.style = style;
	
	// flex item used to separate the left groups items and right grouped items
	UIBarButtonItem *flexItem;
  flexItem = [[UIBarButtonItem alloc] 
              initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
              target:nil
              action:nil];
	
	// create a special tab bar item with a custom image and title
	UIBarButtonItem *infoItem;
  infoItem = [[UIBarButtonItem alloc] 
              initWithImage:[UIImage imageNamed:@"segment_tools.png"]
              style:style
              target:self
              action:@selector(action:)];

	// Set the accessibility label for an image bar item.
	[infoItem setAccessibilityLabel:NSLocalizedString(@"ToolsIcon", @"")];
	
	// create a bordered style button with custom title
	UIBarButtonItem *customItem;
  customItem = [[UIBarButtonItem alloc] 
                initWithTitle:@"Item"
                style:style	// note you can use "UIBarButtonItemStyleDone" to make it blue
                target:self
                action:@selector(action:)];
	
	NSArray *items = [NSArray arrayWithObjects: systemItem, flexItem, customItem, infoItem, nil];
	[self.toolbar setItems:items animated:NO];
	
	[systemItem release];
	[flexItem release];
	[infoItem release];
	[customItem release];
}
*/

- (void)viewDidLoad {
  [super viewDidLoad];
  
  dataSource = [[NKDataSouce alloc] init];
  dataSource.dataArray = [NSMutableArray array];
  [dataSource.dataArray addObject:@"hoge1"];
  [dataSource.dataArray addObject:@"hoge2"];
  [dataSource.dataArray addObject:@"hoge3"];
  [dataSource.dataArray addObject:@"hoge4"];
  [dataSource.dataArray addObject:@"hoge5"];
  [dataSource.dataArray addObject:@"hoge6"];
  [dataSource.dataArray addObject:@"hoge7"];
  [dataSource.dataArray addObject:@"hoge8"];
  [dataSource.dataArray addObject:@"hoge9"];
  [dataSource.dataArray addObject:@"hoge10"];
  
  // Configure the table view.
//  self.tableView.rowHeight = 73.0;
  self.tableView.backgroundColor = DARK_BACKGROUND;
  self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  

  // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
  // self.navigationItem.rightBarButtonItem = self.editButtonItem;

  /*
	// create the UIToolbar at the bottom of the view controller
	//
	toolbar = [UIToolbar new];
	toolbar.barStyle = UIBarStyleDefault;
	
	// size up the toolbar and set its frame
	[toolbar sizeToFit];
	CGFloat toolbarHeight = [toolbar frame].size.height;
	CGRect mainViewBounds = self.view.bounds;
	[toolbar setFrame:CGRectMake(CGRectGetMinX(mainViewBounds),
                               CGRectGetMinY(mainViewBounds) + CGRectGetHeight(mainViewBounds) - (toolbarHeight * 2.0) + 2.0,
                               CGRectGetWidth(mainViewBounds),
                               toolbarHeight)];
	
	[self.view addSubview:toolbar];
	[self createToolbarItems];
   */
  
//  self.tableView.style = UITableViewStyleGrouped;
  [self initWithStyle:UITableViewStyleGrouped];
}


/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
 // Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations.
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 */

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release anything that can be recreated in viewDidLoad or on demand.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return [dataSource.dataArray count];
}


-(void)awakeFromNib
{
  NSLog(@"bundleLoader waked");
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
  static NSString *CellIdentifier = @"RootViewCell";

  RootViewCell *cell = (RootViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  if (cell == nil) {

    // loadNibNamedでnibファイルをロードするとnibファイルに含まれるオブジェクトが
    // 自動生成されてメソッド間のインスタンス変数の接続関係を設定してくれます。
    // nib内の全てのオブジェクトに対して準備ができたよというメッセージawakeFromNibが送られる。
    //
    // @property (nonatomic, assign) IBOutlet RootViewCell *rootCell;
    // XIBではオーナーがこのクラス（RootViewController）に、セルのクラスがRootViewCellとして設定されている必要がある。
    [[NSBundle mainBundle] loadNibNamed:@"RootViewCell" owner:self options:nil];
    cell = rootCell;
    self.rootCell = nil;
  }
    
	// Configure the cell.
  cell.titleLabel.text = [dataSource.dataArray objectAtIndex:indexPath.row];
  cell.useDarkBackground = (indexPath.row % 2 == 0);

  return cell;
}



/*
// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // Navigation logic may go here -- for example, create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController animated:YES];
	// [anotherViewController release];
}
*/


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


- (void)dealloc {
  [rootCell release];
  [dataSource release];
  [super dealloc];
}


@end

